﻿using UnityEngine;
using System.Collections.Generic;

public class PlatformManager : MonoBehaviour {

	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 startPosition;
	public Vector3 minSize, maxSize, minGap, maxGap;
	public float minY, maxY;
	public Material[] materials;
	public PhysicMaterial[] physicMaterials;
	public Booster booster;
	public GameObject pufferFish;
	public GameObject spawnBox;

	public GameObject sbox1;
	
	public GameObject sbox2;

	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	Vector3 viewportRef2 ;
	Vector3 viewportRef ;
	void Start () {
		GameEventManager.GameStart += GameStart;
		GameEventManager.GameOver += GameOver;
		objectQueue = new Queue<Transform>(numberOfObjects);
		for (int i = 0; i < numberOfObjects; i++) {
			objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f, 0f, -100f), Quaternion.identity));
			objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f,10f, -100f), Quaternion.identity));

			objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f, 20f, -100f), Quaternion.identity));

		}
		enabled = false;
	}

	void Update () {
		 viewportRef2 = Camera.main.ViewportToWorldPoint(new Vector3(1,1,Camera.main.nearClipPlane));
		 viewportRef = Camera.main.ViewportToWorldPoint(new Vector3(1,0,Camera.main.nearClipPlane));

		//each blocks y position should be based on pufferfish with a random offset
		minY = minY;// - pufferFish.transform.position.y;
		maxY = maxY;// + pufferFish.transform.position.y;
		if(objectQueue.Peek().localPosition.x + recycleOffset < PufferFish.distanceTraveled){
			Recycle();
		}
	}

	private void Recycle () {
		Vector3 scale = new Vector3(
			Random.Range(minSize.x, maxSize.x),
			Random.Range(minSize.y, maxSize.y),
			Random.Range(minSize.z, maxSize.z));

		Vector3 position = nextPosition;
		position.x += scale.x * 0.5f;
		position.y += scale.y * 0.5f;
		booster.SpawnIfAvailable(position);

		Transform o = objectQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		int materialIndex = Random.Range(0, materials.Length);
		o.renderer.material = materials[materialIndex];
		o.collider.material = physicMaterials[materialIndex];
		objectQueue.Enqueue(o);

		float randomOffset = Random.Range(minGap.y, maxGap.y);

		float testoffsetMin = pufferFish.transform.position.y -  randomOffset;
		float testoffsetMax = pufferFish.transform.position.y +  randomOffset;
		float distance = Random.Range(minGap.y,maxGap.y);
		//randim.rotation
		Vector3 spawnPos;
		Vector3 pPos = pufferFish.transform.position;
		 spawnPos = transform.TransformPoint( Vector3.up * distance ); 
		//spawnPos =new Vector3(pPos.x , pPos.y  - distance, pPos.z);
		//pufferFish.transform.position.y
//		Debug.Log(spawnPos);
		Vector3 v3Pos = Camera.main.ViewportToWorldPoint(new Vector3(1.1f, 0.5f, 10.0f));

		 PufferFish pfRef = pufferFish.GetComponent<PufferFish>();
		float forceRef = pfRef.Force;
	//	pfRef.rigidbody.velocity.y;
		//if(pfRef.rigidbody.velocity.y){

		//}
		//force = mass  *acceleration
		//velocity = distance/time
		//acceleration = 1/2mass * velocity^2
		nextPosition += new Vector3(Random.Range(minGap.x, maxGap.x) + scale.x + 10,0,Random.Range(minGap.z, maxGap.z));

		//Debug.Log(viewportRef + " " + viewportRef2);
		////nextPosition += 
		//nextPosition.y = Random.Range(viewportRef.y -17,viewportRef2.y ); //Random.Range(pPos.y - 10, pPos.y +10);
	//	nextPosition.y = Random.Range(-17,17); //Random.Range(pPos.y - 10, pPos.y +10);
		float multiplier;
		Debug.Log(forceRef);
		if(forceRef > 0){

		}
		Debug.Log(spawnBox.collider.bounds.size.y);
		nextPosition.y = Random.Range(pufferFish.collider.bounds.center.y +5 , pufferFish.collider.bounds.center.y + 5); //Random.Range(pPos.y - 10, pPos.y +10);
		//nextPosition.y = v3Pos.y;

		//Vector3 v3Pos = Camera.main.ViewportToWorldPoint(new Vector3(1.1f, 0.5f, 10.0)); 

		//nextPosition += new Vector3(1,1,0);
		if(nextPosition.y < minY ){
			//nextPosition.y = minGap.y + pPos.y;
			//nextPosition.y = minY + spawnPos.y;
		}
		else if(nextPosition.y > maxY){
			//nextPosition.y = maxGap.y - pPos.y;
			//nextPosition.y = minY - spawnPos.y;
		}

	}
	
	private void GameStart () {
		nextPosition = startPosition;
		for(int i = 0; i < numberOfObjects; i++){
			Recycle();
		}
		enabled = true;
	}

	private void GameOver () {
		enabled = false;
	}
}